<?php
session_start();  
include('../Model/db.php');
include('../Model/Faculty.php');
include('../Model/FacultyProject.php');

function getFacultyById(){
	connectDB();
	$result = Faculty::getFacultyById($_GET['idFaculty']);
	$result[0]['Project'] = FacultyProject::getFacultyProjectByFaculty($_GET['idFaculty']);
	disconnectDB();
	echo json_encode($result);
}

function getFacultyByLimit(){
	connectDB();
	$result = Faculty::getFacultyByLimit($_GET['fromFaculty'], $_GET['toFaculty']);
	disconnectDB();
	echo json_encode($result);
}

function getAll(){
	connectDB();
	$result = Faculty::getFaculty();
	disconnectDB();
	echo json_encode($result);
}

function getCountRow(){
	connectDB();
	$result = Faculty::getCountRow();
	disconnectDB();
	echo json_encode($result);
}

function insertFaculty(){
	connectDB();
	autoCommitOff();
	$faculty_image = '';
	if (!isset($_FILES['image']) || $_FILES['image']["error"] > 0) {
	} else {
		$maxId = Faculty::getMaxId();
		$maxId = $maxId['maxId'] + 1;
	
		$path = $_FILES['image']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		$_FILES['image']["name"]= $maxId.'.'.$ext;
		move_uploaded_file($_FILES['image']["tmp_name"], '../images/faculty/'.$_FILES['image']["name"]);
		$faculty_image	= "server/images/faculty/".$_FILES['image']["name"];
	}
	Faculty::insertFaculty($_POST['name'], $_POST['desc'], $faculty_image, $_POST['department'], $_POST['diploma'], $_POST['process'], $_POST['performance']);
	$result = commitTRAN();
	disconnectDB();
	echo json_encode($result);
}

function updateFaculty(){	
	connectDB();
	autoCommitOff();
	if (!isset($_FILES['image']) || $_FILES['image']["error"] > 0) {
		Faculty::updateFacultyWithoutImage($_POST['id'], $_POST['name'], $_POST['desc'], $_POST['department'], $_POST['diploma'], $_POST['process'], $_POST['performance']);
	} else {
		$path = $_FILES['image']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		$_FILES['image']["name"]= $_POST['id'].'.'.$ext;
		move_uploaded_file($_FILES['image']["tmp_name"], '../images/faculty/'.$_FILES['image']["name"]);
		$faculty_image	= "server/images/faculty/".$_FILES['image']["name"];
		Faculty::updateFaculty($_POST['id'], $_POST['name'], $_POST['desc'], $faculty_image, $_POST['department'], $_POST['diploma'], $_POST['process'], $_POST['performance']);
	}
	
	$result = commitTRAN();
	disconnectDB();
	echo json_encode($result);
}

function deleteFaculty(){
	connectDB();
	autoCommitOff();
	Faculty::deleteFaculty($_POST['id']);
	$result = commitTRAN();
	disconnectDB();
	echo json_encode($result);
}

if($_POST){
	if(isset($_SESSION['wkadmin'])){
		if(isset($_POST['action'])){
			$action = $_POST['action'];
			switch($action){
				case 'insert' : insertFaculty(); break;
				case 'update' : updateFaculty(); break;
				case 'delete' : deleteFaculty(); break;
				default: echo json_encode(-1); break;
			}
		} else {
			echo json_encode(-1);
		}
	} else {
		echo json_encode(-1);
	}
}

if($_GET){
	if(isset($_GET['action'])){
		$action = $_GET['action'];
		switch($action){
			case 'getById' : getFacultyById(); break;
			case 'getAll': getAll(); break;
			case 'getByLimit' : getFacultyByLimit(); break;
			case 'getCountRow' : getCountRow(); break;
			default: echo json_encode(-1); break;
		}
	} else {
		echo json_encode(-1);
	}
}
?>